package com.xrit;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Daoying
 * creationTime: 2022/11/15 14:36
 */
public class Main {
    public static void main(String[] args) throws Exception {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("D:\\ftl"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("test.ftl");
        //5.创建数据模型
        Map map = new HashMap();
        map.put("name", "小七");
        map.put("message", "欢迎来到火星！");
        map.put("linkman", "周先生");
        map.put("success", false);

        List goodsList = new ArrayList();

        Map goods1 = new HashMap();
        goods1.put("name", "苹果");
        goods1.put("price", 6.6);

        Map goods2 = new HashMap();
        goods2.put("name", "西瓜");
        goods2.put("price", 1.1);

        goodsList.add(goods1);
        goodsList.add(goods2);

        map.put("goodsList", goodsList);

        //6.创输出文件路径对象
        Writer out = new FileWriter(new File("D:\\test.html"));
        //7.将数据填充到模板
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }
}
